/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EmployeeEditor.java
 *
 * Created on 19-May-2009, 10:45:25
 */
package TimeRecording.forms;

import TimeRecording.business.PasswordEncryption;
import TimeRecording.tables.Badge;
import TimeRecording.tables.Employee;
import TimeRecording.widgets.AuthorisationLevelComboBox;
import TimeRecording.widgets.EmployeeStatusComboBox;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.jdesktop.application.Action;

/**
 *
 * @author cbegg
 */
public class EmployeeEditor extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;

    /** Creates new form EmployeeEditor */
    public EmployeeEditor(EmployeesDialog parent, boolean modal) {
        super(parent, modal);
        myParent = parent;
        initComponents();
    }

    protected boolean editMode;

    /**
     * Get the value of editMode
     *
     * @return the value of editMode
     */
    public boolean isEditMode() {
        return editMode;
    }

    /**
     * Set the value of editMode
     *
     * @param editMode new value of editMode
     */
    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    protected Employee currentRecord;

    /**
     * Get the value of employee
     *
     * @return the value of employee
     */
    public Employee getCurrentRecord() {
        return currentRecord;
    }

    /**
     * Set the value of employee
     *
     * @param employee new value of employee
     */
    @SuppressWarnings("unchecked")
    public void setCurrentRecord(Employee employee) {

        Employee oldRecord = this.currentRecord;
        this.currentRecord = employee;
        propertyChangeSupport.firePropertyChange("currentRecord", oldRecord, currentRecord);

        badgesList.clear();
        if (employee != null) {
            badgesQuery.setParameter("employeeId", employee);
            badgesList.addAll(badgesQuery.getResultList());
        }
        
        // Set focus to first input field
        forenameText.requestFocus();
    }

    protected boolean employeeConfirmed;

    /**
     * Get the value of employeeConfirmed
     *
     * @return the value of employeeConfirmed
     */
    public boolean isEmployeeConfirmed() {
        return employeeConfirmed;
    }

    /**
     * Set the value of employeeConfirmed
     *
     * @param employeeConfirmed new value of employeeConfirmed
     */
    public void setEmployeeConfirmed(boolean employeeConfirmed) {
        this.employeeConfirmed = employeeConfirmed;
    }

    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    /**
     * Add PropertyChangeListener.
     *
     * @param listener
     */
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    /**
     * Remove PropertyChangeListener.
     *
     * @param listener
     */
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        entityManager = myParent.getEntityManager();
        badgesQuery = entityManager.createNamedQuery("Badge.findByEmployeeId");
        badgesList = org.jdesktop.observablecollections.ObservableCollections.observableList(new ArrayList<TimeRecording.tables.Badge>());
        forenameLabel = new javax.swing.JLabel();
        forenameText = new javax.swing.JTextField();
        surnameLabel = new javax.swing.JLabel();
        surnameText = new javax.swing.JTextField();
        usernameLabel = new javax.swing.JLabel();
        usernameText = new javax.swing.JTextField();
        payrollReferenceLabel = new javax.swing.JLabel();
        payrollReferenceText = new javax.swing.JTextField();
        passwordLabel = new javax.swing.JLabel();
        passwordField = new javax.swing.JPasswordField();
        verifyPasswordLabel = new javax.swing.JLabel();
        verifyPasswordField = new javax.swing.JPasswordField();
        authorisationLevelLabel = new javax.swing.JLabel();
        authorisationLevelCombo = new AuthorisationLevelComboBox();
        badgeNumberLabel = new javax.swing.JLabel();
        badgesPane = new javax.swing.JScrollPane();
        badgesTable = new javax.swing.JTable();
        buttonPanel = new javax.swing.JPanel();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getResourceMap(EmployeeEditor.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setMinimumSize(new java.awt.Dimension(600, 240));
        setModal(true);
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        forenameLabel.setLabelFor(forenameText);
        forenameLabel.setText(resourceMap.getString("forenameLabel.text")); // NOI18N
        forenameLabel.setName("forenameLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 5, 4);
        getContentPane().add(forenameLabel, gridBagConstraints);

        forenameText.setName("forenameText"); // NOI18N

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.forename}"), forenameText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        forenameText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                forenameTextFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(10, 4, 5, 4);
        getContentPane().add(forenameText, gridBagConstraints);

        surnameLabel.setLabelFor(surnameText);
        surnameLabel.setText(resourceMap.getString("surnameLabel.text")); // NOI18N
        surnameLabel.setName("surnameLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(surnameLabel, gridBagConstraints);

        surnameText.setName("surnameText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.surname}"), surnameText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        surnameText.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                surnameTextFocusLost(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(surnameText, gridBagConstraints);

        usernameLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        usernameLabel.setLabelFor(usernameText);
        usernameLabel.setText(resourceMap.getString("usernameLabel.text")); // NOI18N
        usernameLabel.setName("usernameLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 4);
        getContentPane().add(usernameLabel, gridBagConstraints);

        usernameText.setName("usernameText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.username}"), usernameText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(usernameText, gridBagConstraints);

        payrollReferenceLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        payrollReferenceLabel.setLabelFor(payrollReferenceText);
        payrollReferenceLabel.setText(resourceMap.getString("payrollReferenceLabel.text")); // NOI18N
        payrollReferenceLabel.setName("payrollReferenceLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 4);
        getContentPane().add(payrollReferenceLabel, gridBagConstraints);

        payrollReferenceText.setName("payrollReferenceText"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.payrollReference}"), payrollReferenceText, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(payrollReferenceText, gridBagConstraints);

        passwordLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        passwordLabel.setLabelFor(passwordField);
        passwordLabel.setText(resourceMap.getString("passwordLabel.text")); // NOI18N
        passwordLabel.setName("passwordLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(10, 4, 5, 4);
        getContentPane().add(passwordLabel, gridBagConstraints);

        passwordField.setText(resourceMap.getString("passwordField.text")); // NOI18N
        passwordField.setName("passwordField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 10);
        getContentPane().add(passwordField, gridBagConstraints);

        verifyPasswordLabel.setLabelFor(verifyPasswordField);
        verifyPasswordLabel.setText(resourceMap.getString("verifyPasswordLabel.text")); // NOI18N
        verifyPasswordLabel.setName("verifyPasswordLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(verifyPasswordLabel, gridBagConstraints);

        verifyPasswordField.setText(resourceMap.getString("verifyPasswordField.text")); // NOI18N
        verifyPasswordField.setName("verifyPasswordField"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 10);
        getContentPane().add(verifyPasswordField, gridBagConstraints);

        authorisationLevelLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        authorisationLevelLabel.setLabelFor(authorisationLevelCombo);
        authorisationLevelLabel.setText(resourceMap.getString("authorisationLevelLabel.text")); // NOI18N
        authorisationLevelLabel.setName("authorisationLevelLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 10, 5, 4);
        getContentPane().add(authorisationLevelLabel, gridBagConstraints);

        authorisationLevelCombo.setName("authorisationLevelCombo"); // NOI18N

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${currentRecord.authorisationLevel}"), authorisationLevelCombo, org.jdesktop.beansbinding.BeanProperty.create("selectedItem"));
        bindingGroup.addBinding(binding);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.weightx = 0.5;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(authorisationLevelCombo, gridBagConstraints);

        badgeNumberLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        badgeNumberLabel.setText(resourceMap.getString("badgeNumberLabel.text")); // NOI18N
        badgeNumberLabel.setName("badgeNumberLabel"); // NOI18N
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 4);
        getContentPane().add(badgeNumberLabel, gridBagConstraints);

        badgesPane.setName("badgesPane"); // NOI18N
        badgesPane.setPreferredSize(new java.awt.Dimension(50, 20));

        badgesTable.setName("badgesTable"); // NOI18N

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, badgesList, badgesTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${badgeId}"));
        columnBinding.setColumnName("Badge Id");
        columnBinding.setColumnClass(Integer.class);
        columnBinding.setEditable(false);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        badgesPane.setViewportView(badgesTable);
        badgesTable.getColumnModel().getColumn(0).setHeaderValue(resourceMap.getString("badgesTable.columnModel.title0")); // NOI18N

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 4, 5, 10);
        getContentPane().add(badgesPane, gridBagConstraints);

        buttonPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(4, 0, 4, 0));
        buttonPanel.setName("buttonPanel"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(TimeRecording.TimeRecordingApp.class).getContext().getActionMap(EmployeeEditor.class, this);
        saveButton.setAction(actionMap.get("save")); // NOI18N
        saveButton.setText(resourceMap.getString("saveButton.text")); // NOI18N
        saveButton.setName("saveButton"); // NOI18N
        buttonPanel.add(saveButton);

        cancelButton.setAction(actionMap.get("cancel")); // NOI18N
        cancelButton.setText(resourceMap.getString("cancelButton.text")); // NOI18N
        cancelButton.setName("cancelButton"); // NOI18N
        buttonPanel.add(cancelButton);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 5;
        gridBagConstraints.gridwidth = 4;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        getContentPane().add(buttonPanel, gridBagConstraints);

        bindingGroup.bind();

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-637)/2, (screenSize.height-257)/2, 637, 257);
    }// </editor-fold>//GEN-END:initComponents

    private void surnameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_surnameTextFocusLost
        setDefaultUsername();
    }//GEN-LAST:event_surnameTextFocusLost

    private void forenameTextFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_forenameTextFocusLost
        setDefaultUsername();
    }//GEN-LAST:event_forenameTextFocusLost

    @Action
    public void cancel() {
        setEmployeeConfirmed(false);
        setVisible(false);
    }

    @Action
    @SuppressWarnings("unchecked")
    public void save() {

        // Verify fields marked as Unique are not duplicated in this record
        
        boolean bValid = true;
        Employee employee;
        List<Employee> employeeList;

        // Check for blank forename/surname.  At least one must be filled in

        if ((forenameText == null || forenameText.getText().equals(""))
                && (surnameText == null || surnameText.getText().equals(""))) {

            bValid = false;
            JOptionPane.showMessageDialog(this,
                    "Forename and surname cannot both be blank",
                    "Input Error",
                    JOptionPane.ERROR_MESSAGE);

            forenameText.requestFocus();
        }

        // Check payroll reference is not being duplicated

        if (bValid && payrollReferenceText.getText().length() > 0) {

            employeeList = entityManager.createNamedQuery("Employee.findByPayrollReference").
                    setParameter("payrollReference", this.payrollReferenceText.getText()).getResultList();

            // Is this payroll reference associated with an employee?

            if (employeeList.size() != 0) {

                // It can only be associated with a single employee (that's what
                // we're checking for) so we only need to look at the first entry

                employee = employeeList.get(0);

                // Make sure we are that employee

                if (!editMode ||
                        (currentRecord.getEmployeeId() != employee.getEmployeeId())) {

                    bValid = false;
                    JOptionPane.showMessageDialog(this,
                            "Payroll Reference already in use by " + employee.toString(),
                            "Input Error",
                            JOptionPane.ERROR_MESSAGE);

                    payrollReferenceText.requestFocus();
                }
            }
        }

        if (bValid) {

            // Ensure the username is not blank

            if (usernameText.getText().equals("")) {

                bValid = false;
                JOptionPane.showMessageDialog(this,
                        "Username cannot be blank",
                        "Input Error",
                        JOptionPane.ERROR_MESSAGE);

                usernameText.requestFocus();

            } else {

                // Ensure the username is not already allocated

                employeeList = entityManager.createNamedQuery("Employee.findByUsername").
                        setParameter("username", this.usernameText.getText()).getResultList();

                if (!employeeList.isEmpty()) {

                    // There can only be one employee with this username (that's
                    // what we're checking for)  so we only need to look at the
                    // first entry

                    employee = employeeList.get(0);

                    // Make sure we are that employee

                    if (!editMode ||
                            (currentRecord.getEmployeeId() != employee.getEmployeeId())) {

                        bValid = false;
                        JOptionPane.showMessageDialog(this,
                                "Username already in use by " + employee.toString(),
                                "Input Error",
                                JOptionPane.ERROR_MESSAGE);

                        usernameText.requestFocus();
                    }
                }
            }
        }

        if (bValid) {

            // Now check passwords match (we currently allow blank passwords)

            if (!Arrays.equals(passwordField.getPassword(), verifyPasswordField.getPassword())) {

                bValid = false;
                JOptionPane.showMessageDialog(this,
                        "Passwords don't match, try again",
                        "Input Error",
                        JOptionPane.ERROR_MESSAGE);

                passwordField.requestFocus();

            } else if (!(new String(passwordField.getPassword())).equals("")) {

                // Encrypt password and store as 128 byte digest
                String sPassword = new String(passwordField.getPassword());
                String sPasswordDigest = PasswordEncryption.Password(sPassword);

                // Is the password digest the same as before?
                
                if (!sPasswordDigest.equals(currentRecord.getPassword())) {

                    // We need to set this explicity as the password form
                    // controls are not bound to the current record
                    currentRecord.setPassword(sPasswordDigest);
                }
            }
        }

        if (bValid) {

            setEmployeeConfirmed(true);
            setVisible(false);
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox authorisationLevelCombo;
    private javax.swing.JLabel authorisationLevelLabel;
    private javax.swing.JLabel badgeNumberLabel;
    private java.util.List<TimeRecording.tables.Badge> badgesList;
    private javax.swing.JScrollPane badgesPane;
    private javax.persistence.Query badgesQuery;
    private javax.swing.JTable badgesTable;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JButton cancelButton;
    private javax.persistence.EntityManager entityManager;
    private javax.swing.JLabel forenameLabel;
    private javax.swing.JTextField forenameText;
    private javax.swing.JPasswordField passwordField;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JLabel payrollReferenceLabel;
    private javax.swing.JTextField payrollReferenceText;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel surnameLabel;
    private javax.swing.JTextField surnameText;
    private javax.swing.JLabel usernameLabel;
    private javax.swing.JTextField usernameText;
    private javax.swing.JPasswordField verifyPasswordField;
    private javax.swing.JLabel verifyPasswordLabel;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    private EmployeesDialog myParent;

    private void setDefaultUsername() {

        String forename = forenameText.getText();
        String surname = surnameText.getText();
        String username = usernameText.getText();

        if (forename != null && forename.length() > 1 &&
                surname != null && surname.length() > 1 &&
                (username == null || username.length() == 0)) {
            username = (forename.substring(0, 1) + surname).toLowerCase();
            usernameText.setText(username);
        }
    }
}